import Vue from 'vue'
import VueRouter from 'vue-router'

// import Login from '../components/Login'
// import Home from '../components/Home'
// import Welcome from '../components/Welcome'
const Login = () => import(/* webpackChunkName: "login_home_welcome" */ '../components/Login')
const Home = () => import(/* webpackChunkName: "login_home_welcome" */ '../components/Home')
const Welcome = () => import(/* webpackChunkName: "login_home_welcome" */ '../components/Welcome')

// import Users from '../components/user/Users'
// import Rights from '../components/user/Rights'
// import Roles from '../components/user/Roles'
const Users = () => import(/* webpackChunkName: "users_rights_roles" */ '../components/user/Users')
const Rights = () => import(/* webpackChunkName: "users_rights_roles" */ '../components/user/Rights')
const Roles = () => import(/* webpackChunkName: "users_rights_roles" */ '../components/user/Roles')

// import Categories from '../components/goods/Categories'
// import Params from '../components/goods/Params'
// import Goods from '../components/goods/Goods'
const Categories = () => import(/* webpackChunkName: "categories_params_goods" */ '../components/goods/Categories')
const Params = () => import(/* webpackChunkName: "categories_params_goods" */ '../components/goods/Params')
const Goods = () => import(/* webpackChunkName: "categories_params_goods" */ '../components/goods/Goods')

// import Add from '../components/goods/Add'
// import Order from '../components/order/Order'
// import Report from '../components/report/Report'
const Add = () => import(/* webpackChunkName: "add_order_report" */ '../components/goods/Add')
const Order = () => import(/* webpackChunkName: "add_order_report" */ '../components/order/Order')
const Report = () => import(/* webpackChunkName: "add_order_report" */ '../components/report/Report')

const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

Vue.use(VueRouter)

const routes = [
  { path: '/', redirect: '/login' },
  { path: '/login', component: Login },
  {
    path: '/home',
    component: Home,
    redirect: '/welcome',
    children: [
      { path: '/welcome', component: Welcome },
      { path: '/users', component: Users },
      { path: '/rights', component: Rights },
      { path: '/roles', component: Roles },
      { path: '/categories', component: Categories },
      { path: '/params', component: Params },
      { path: '/goods', component: Goods },
      { path: '/goods/add', component: Add },
      { path: '/orders', component: Order },
      { path: '/reports', component: Report }
    ]
  }
]

const router = new VueRouter({
  routes
})

// 路由导航守卫
router.beforeEach((to, from, next) => {
  // to：路由到哪儿去，from：从哪儿来的路由，next：放行
  if (to.path === '/login') {
    return next()
  }
  const tokenStr = window.sessionStorage.getItem('token')
  if (!tokenStr) return next('/login')
  next()
})

export default router
